package com.example.friendcash.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import com.example.friendcash.R;
import com.example.friendcash.parameters.IntentConstants;

import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT;

public class NewEventActivity extends Activity {

    private EditText nameField;
    private EditText commentField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_event);
        nameField = (EditText) findViewById(R.id.event_name);
        commentField = (EditText) findViewById(R.id.event_comment);
        final Button createEventButton = (Button) findViewById(R.id.create_event_button);
        createEventButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (nameField.getText().length() <= 0) {
                    nameField.setError(getString(R.string.enter_name));
                } else {
                    final Intent intent = new Intent();
                    intent.putExtra(IntentConstants.NAME, nameField.getText().toString());
                    intent.putExtra(IntentConstants.COMMENT, commentField.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        setFocusToElement(nameField);
    }

    private void setFocusToElement(View view) {
        final Context applicationContext = getApplicationContext();
        final InputMethodManager inputManager = (InputMethodManager) applicationContext.getSystemService(INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, SHOW_IMPLICIT);
    }

}
